package com.liuyfly.leetcode.study;

/**
 * @author liuyangfang at 2022-08-29 02:26
 */
public class LongestCommonPrefix {
    public static String longestCommonPrefix(String[] strs) {
        if (strs.length==1||strs[0].length()==0){
            return strs[0];
        }
        String result = "";
        boolean match = true;
        for (int i=1;i<=strs[0].length();i++){
            result = strs[0].substring(0,i);
            for (int j=1;j<strs.length;j++){
                if (!strs[j].startsWith(result)){
                    match=false;
                    break;
                }
            }
            if (!match){
                break;
            }
        }
        if (match){
            return result;
        } else {
            return result.substring(0, result.length() - 1);
        }
    }

    public static void main(String[] args) {
        String[] strs = {"flower","flower"};
        System.out.println(longestCommonPrefix(strs));
    }
}
